package com.zzu.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zzu.R;
import com.zzu.model.bean.Delivery;
import com.zzu.utils.ChangeActivity;

import java.util.List;

/**
 * @description:
 * @author: bytedance
 * @date: 2021-05-08 18:22
 */
public class OneAdapter extends RecyclerView.Adapter<OneAdapter.ViewHolder> {

    private static final String TAG = OneAdapter.class.getName();
    private Context mContext;
    private List<Delivery> datas;

    public OneAdapter(Context context, List<Delivery> datas) {
        mContext = context;
        this.datas = datas;
        Log.i(TAG,"初始化的数据为："+datas);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_one, null);
        return new ViewHolder(view);
    }

    //进行数据绑定
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //获取该位置的数据
        Delivery delivery = datas.get(position);

        holder.tv_one_packageId.setText(delivery.getPackageId());
        holder.tv_one_receiver.setText(delivery.getReceiver());
        holder.tv_one_receiverTel.setText(delivery.getReceiverTel());
        holder.tv_one_receiverAddress.setText(delivery.getReceiverAddr());

        //设置监听事件

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_one_packageId;
        TextView tv_one_updateTime;
        TextView tv_one_receiver;
        TextView tv_one_receiverTel;
        TextView tv_one_receiverAddress;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_one_packageId = itemView.findViewById(R.id.tv_one_packageId);
            tv_one_updateTime = itemView.findViewById(R.id.tv_one_updateTime);
            tv_one_receiver = itemView.findViewById(R.id.tv_one_receiver);
            tv_one_receiverTel = itemView.findViewById(R.id.tv_one_receiverTel);
            tv_one_receiverAddress = itemView.findViewById(R.id.tv_one_receiverAddress);

            itemView.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    Log.i(TAG,"包裹id为："+datas.get(getLayoutPosition()).getPackageId()+"包裹的位置"+getLayoutPosition());

                    String packageId = datas.get(getLayoutPosition()).getPackageId();
                    //进行页面跳转
                    ChangeActivity.ChangeExpressInfo(mContext,packageId);
                    //刷新数据
                    notifyDataSetChanged();
                }
            });

        }

    }

}
